﻿        var count = 0;

        function findRowIdxForButton(buttonId) {
        
            var button = document.getElementById(buttonId);
            var cell = button.parentNode;
            var row = cell.parentNode;
            var idx = row.rowIndex;
            
            return idx;
        }

        function swapRows(fromRow, toRow) {
        	var fromInputs = fromRow.getElementsByTagName("input");
        	var toInputs = toRow.getElementsByTagName("input");

        	var checked = toInputs[1].checked;
        	toInputs[1].checked = fromInputs[1].checked;
        	fromInputs[1].checked = checked;

        	var value = toInputs[2].value;
        	toInputs[2].value = fromInputs[2].value;
        	fromInputs[2].value = value;

        	value = toInputs[3].value;
        	toInputs[3].value = fromInputs[3].value;
        	fromInputs[3].value = value;
        }
        
        function upRow(buttonId) {
            var checklist = document.getElementById("checklist");
            var idx = findRowIdxForButton(buttonId);

            if (idx != 1) {
            	swapRows(checklist.rows[idx], checklist.rows[idx - 1]);
            }
        }

        function downRow(buttonId) {
            var checklist = document.getElementById("checklist");
            var idx = findRowIdxForButton(buttonId);

            if (idx < checklist.rows.length - 1) {
            	swapRows(checklist.rows[idx + 1], checklist.rows[idx]);
            }
        }
        
        function removeRow(buttonId) {
            var checklist = document.getElementById("checklist");
            var idx = findRowIdxForButton(buttonId);
            checklist.deleteRow(idx);
        }

        function insertRow(idx, values) {
            if (values)
            	wave.log("insertRow(" + idx + ", " + values.length + ")");
            else
            	wave.log("insertRow(" + idx + ", null)");
        	var checklist = document.getElementById("checklist");
        	
        	checklist.insertRow(idx);
        	count = count + 1;
        	
        	var newRow = checklist.rows[idx];
        	var checkCell = newRow.insertCell(0);
        	var taskCell = newRow.insertCell(1);
        	var commentCell = newRow.insertCell(2);
        	var addCell = newRow.insertCell(3);

        	var buttonId = "button" + count;

        	checkCell.setAttribute("style", "padding-top: 3px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px;");
        	taskCell.setAttribute("style", "padding-top: 3px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px;");
        	commentCell.setAttribute("style", "padding-top: 3px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px;");

        	if (wave.getMode() == wave.Mode.EDIT) {
        		checkCell.innerHTML = '<input id=' + '"' + buttonId + '"' + ' type="button" value="-" onclick="removeRow(' + "'" + buttonId + "'" + ')" /><input type="checkbox"/>';
        	}
        	else {
        		checkCell.innerHTML = '<input type="checkbox"/>';
        	}
        	

        	if (wave.getMode() == wave.Mode.EDIT) {
        		taskCell.innerHTML = '<input type="text" style="width:100%;"/>';
        	}
        	else {
        		taskCell.innerHTML = values[1];
        	}

        	commentCell.innerHTML = '<input type="text" style="width:100%;"/>';

        	if (wave.getMode() == wave.Mode.EDIT) {
        		addCell.setAttribute("style", "padding-top: 3px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px;");
        		addCell.innerHTML =
                '<input type="button" value="^" onclick="upRow(' + "'" + buttonId + "'" + ')" />' +
                '<input type="button" value="v" onclick="downRow(' + "'" + buttonId + "'" + ')" />' +
                '<input id=' + '"' + buttonId + '"' + ' type="button" value="+" onclick="addRow(' + "'" + buttonId + "'" + ')" />';

        		var inputs = newRow.getElementsByTagName("input");
        		if (values[0] == 'true')
        			inputs[1].checked = true;
        		else
        			inputs[1].checked = false;
        		inputs[2].value = values[1];
        		inputs[3].value = values[2];
        	}
        	else {
        		var inputs = newRow.getElementsByTagName("input");
        		if (values[0] == 'true')
        			inputs[0].checked = true;
        		else
        			inputs[0].checked = false;
        		inputs[1].value = values[2];
        	}
        }
        
        function addRow(buttonId) {
        	var checklist = document.getElementById("checklist");
        	var idx = findRowIdxForButton(buttonId);
        	insertRow(idx + 1, null);
        }

        function initializeFromState() {
        	var keys = wave.getState().getKeys();
        	wave.log("initializeFromState: [keys: " + keys.length + "]");
        	for (var idx = 1; idx <= keys.length; idx++) {
        		var state = wave.getState().get(idx.toString());
        		wave.log("key: " + idx + " - " + state);
        		var values = state.split("\n");
        		wave.log("State has " + values.length + " values: " + state);
        		insertRow(idx, values);
        	}
        }

        function saveState() {
        	wave.log("saveState");
        	var checklist = document.getElementById('checklist');
        	            
            var idx = 1;
        	for (; idx < checklist.rows.length; idx++) {
        		var rowInputs = checklist.rows[idx].getElementsByTagName("input");
        		wave.log("rowInputs: " + rowInputs.length);
        		
        		var stateValue = null;
        		if (rowInputs.length == 2) {
        			wave.log("-- " + rowInputs[0].checked);
        			wave.log("-- " + checklist.rows[idx].cells[1].innerHTML);
        			wave.log("-- " + rowInputs[1].value);

        			stateValue = rowInputs[0].checked + "\n" + checklist.rows[idx].cells[1].innerHTML + "\n" + rowInputs[1].value;
        		}
        		else {
        			wave.log("-- " + rowInputs[1].checked);
        			wave.log("-- " + rowInputs[2].value);
        			wave.log("-- " + rowInputs[3].value);
        			stateValue = rowInputs[1].checked + "\n" + rowInputs[2].value + "\n" + rowInputs[3].value;
        		}
      			wave.getState().submitValue(idx, stateValue);
      		}
      		
      		var keys = wave.getState().getKeys();
      		for (; idx < keys.length; idx++)
      			wave.getState().submitValue(idx, null); 
        	
        }

        function resetTable() {
        	var checklist = document.getElementById('checklist');
        	wave.log("resetTable: [rows: " + checklist.rows.length + "][count: " + count + "]");
        	count = 0;
        	while (checklist.rows.length > 1)
        		checklist.deleteRow(1);
        }
        
        function initializeForEditing() {
        	wave.log("initializeForEditing");
        	saveState();
        	
            var addRowButton = document.getElementById("Add0");
            addRowButton.style.visibility = 'visible';
        }

        function initializeForViewing() {
        	wave.log("initializeForViewing");
        	saveState();

            var addRowButton = document.getElementById("Add0");
            addRowButton.style.visibility = 'hidden';
        }

        function stateUpdated() {
        	wave.log("stateUpdated");
        	resetTable();
        	initializeFromState();
        }

        function modeChanged() {
        	wave.log("modeChanged: " + wave.getMode()); 
        	if (wave.getMode() == wave.Mode.EDIT)
        		initializeForEditing();
        	else if (wave.getMode() == wave.Mode.VIEW)
        		initializeForViewing();
        }
        
        function init() {
          if (wave && wave.isInWaveContainer()) {
          	wave.setStateCallback(stateUpdated);
          	wave.setModeCallback(modeChanged)
          }
        }

        gadgets.util.registerOnLoadHandler(init);
        
       
